package mest.service.LGF.PlanPurchaseMoney.Impl;

import mest.VO.LGF.PlanPurchaseMoney.plan_purchasemoneyrecord;
import mest.dao.LGF.PlanPurchaseMoney.IPurchaseMoneyDao;
import mest.service.LGF.PlanPurchaseMoney.IPlanPurchaseMoneyService;
import mest.util.mestUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-12-18 15:12
 */
@Service
public class IPlanPurchaseMoneyServiceImpl implements IPlanPurchaseMoneyService {
    @Resource
    HttpServletRequest request;
    //    String theirDb = mestUtil.getCompanyDB(request);
    @Resource
    private IPurchaseMoneyDao ipmd;


    @Override
    public Map baseSelectNofinish(int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
         Map map = new HashMap();
        int count = ipmd.countNOFinish(theirDb);

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",ipmd.selectAllNOFinish(theirDb,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public Map baseSelectFinish(int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = ipmd.countFinish(theirDb);

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",ipmd.selectAllFinish(theirDb,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public int addMoneyRecord(plan_purchasemoneyrecord ppmr) {
        String theirDb = mestUtil.getCompanyDB(request);
        ppmr.setTheirDb(theirDb);
        int i = -2;
        int i1 = -2;
        int i2 = -2;
        try {
            i =  ipmd.insertByPPMR(ppmr);
            //此处需要插入财务流采购收款
             i1 = ipmd.insertFinancial_Purchasemoney(ppmr);
             //更新字典操作
            i2 = ipmd.updateFinancial_PurchasemoneyNumber(theirDb);

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(i == -2 || i1 == -2 || i2 == -2){
                return -2;
            }
        }
        return i;
    }

    @Override
    public int insertPlanPurchaseMoneyOrdrByPOID(int poId) {
        String theirDb = mestUtil.getCompanyDB(request);
        int i1 = -2;
        int i2 = -2;
        try {
             i1 = ipmd.insertPlanPurchaseMoneyOrdrByPOID(theirDb, poId);
             i2 = ipmd.updatePlanPurchaseMoneyNumber(theirDb);

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if( i1 == -2 || i2 == -2){
                return -2;
            }
        }
        return i1;
    }

    @Override
    public Map selectPastRecord(int ppmId,int page,int limit) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = ipmd.selectPastRecordCount(theirDb);

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",ipmd.selectPastRecord(theirDb,ppmId,(page-1)*limit,limit));
        return map;
    }

    @Override
    public int updateByPPMId(int ppmId, int status) {
        String theirDb = mestUtil.getCompanyDB(request);
        int i =-2;
        try {
            i = ipmd.updateByPPMId(theirDb,ppmId,status);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }
}
